/**
 * 
 */
package data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import domain.LocalPrinter;
import domain.NetworkPrinter;
import domain.Printer;

/**
 * @author grupo22
 *
 */
public class JDBCPrinterDAO implements IPrinterDAO {

	/* (non-Javadoc)
	 * @see data.IPrinterDAO#select(java.sql.Connection, java.lang.String, domain.Printer)
	 */
	@Override
	public Printer select(Connection con, String oid) {
		String sql = "SELECT * FROM Printer WHERE (OIDProduct = ? )";
        PreparedStatement stmt = null;
        ResultSet rs = null;
        Printer p = null;

        try {
            stmt = con.prepareStatement(sql);
            stmt.setString(1, oid);

            rs = stmt.executeQuery();

            rs.next();
            if (rs.getString("objectType").compareTo("network")==0){
            	p = new NetworkPrinter();
            	((NetworkPrinter) p).setSnmpCapable(rs.getBoolean("snmpCapable"));
            	((NetworkPrinter) p).setDebianComp(rs.getBoolean("debianComp"));
            } else if (rs.getString("objectType").compareTo("local")==0){
            	p = new LocalPrinter();
            	((LocalPrinter) p).setUsbCableInc(rs.getBoolean("usbCableInc"));
            	((LocalPrinter) p).setGuadalinexComp(rs.getBoolean("guadalinexComp"));
            }
            p.setPpm(rs.getInt("ppm"));
            p.setDuplex(rs.getBoolean("duplex"));
            p.setNic(rs.getBoolean("nic"));
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) { }
        }
        return p;
	}
}
